//
//  CreateViewController.m
//  moon
//
//  Created by beyself on 16/6/19.
//  Copyright © 2016年 beyself. All rights reserved.
//
#import <Photos/PHImageManager.h>
#import "Utils.h"
#import "UIImageView+AFNetworking.h"
#import "CreateViewController.h"
#import "MainViewController.h"
#import "AFHTTPSessionManager.h"
#import "QBImagePickerController.h"

@interface CreateViewController ()<UITextViewDelegate,QBImagePickerControllerDelegate,UIScrollViewDelegate>

@end

@implementation CreateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"发布班级圈";

    UIScrollView* scrollview = [[UIScrollView alloc] init];
    scrollview.tag = 10000;
    scrollview.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    scrollview.showsVerticalScrollIndicator = NO;
    scrollview.delegate = self;
    scrollview.backgroundColor = WHITE;
    scrollview.contentSize = CGSizeMake(0, SCREEN_HEIGHT);
    [self.view addSubview:scrollview];
    
    BOOL isAdmin = NO;
    NSDictionary* item = APPDELEGATE.mMemberInfo;
    if([item[@"is_sys"] intValue] == 1) isAdmin = YES;
    _mSelectClass = [[NSMutableArray alloc] init];
    _mImgData = [[NSMutableArray alloc] init];
    _mImg = [[NSMutableArray alloc] init];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送"
                                                                              style:UIBarButtonItemStylePlain
                                                                             target:self
                                                                             action:@selector(modifyName)];
    UIView* subview = [[UIView alloc] init];
    subview.frame = CGRectMake(0, 0, SCREEN_WIDTH, 54 + 88);
    subview.backgroundColor = WHITE;
    [scrollview addSubview:subview];
    
    UIImageView* imageview = [[UIImageView alloc] init];
    imageview.frame = CGRectMake(10, 9, 36, 36);
    imageview.contentMode = UIViewContentModeScaleAspectFill;
    imageview.layer.cornerRadius = 18;
    imageview.layer.masksToBounds = YES;
    
    
    BOOL isteacher = NO;
    NSArray* child = item[@"children"];
    NSArray* classes = item[@"classes"];
    for(NSDictionary* dic in classes)
    {
        if([dic[@"is_teacher"] intValue] > 0)
        {
            isteacher = YES;
            break;
        }
    }
    if(isteacher)
        [imageview setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_URL,item[@"avatar_path"]]]
                  placeholderImage:[UIImage imageNamed:@"ic_teacher"]];
    else
        [imageview setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_URL,item[@"avatar_path"]]]
                  placeholderImage:[UIImage imageNamed:@"ic_family"]];
    
    [subview addSubview:imageview];
    
    UILabel* label = [[UILabel alloc] init];
    label.frame = CGRectMake(56, 9, 240, 36);
    label.font = FONT_15;
    label.textColor = BLUE;
    label.text = item[@"username"];
    [subview addSubview:label];
    
    int width = (SCREEN_WIDTH - 40)/3;
    for (int i = 0; i < 3; i++)
    {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 2000 + i;
        if(!isAdmin)
        {
            if(i > 0) button.hidden = YES;
            else  button.selected = YES;
            button.frame = CGRectMake(10, 60, width, 32);
        }
        else
        {
            if(i == 0) button.selected = YES;
            button.frame = CGRectMake(10 + (width + 10)*i, 60, width, 32);
        }
        button.titleLabel.font = FONT_15;
        button.layer.cornerRadius = 16;
        button.layer.masksToBounds = YES;
        [button setTitleColor:GRAY forState:UIControlStateNormal];
        [button setTitleColor:WHITE forState:UIControlStateSelected];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateNormal];
        [button setBackgroundImage:[Utils imageFromColor:ORANGE] forState:UIControlStateSelected];
        if(i == 1)
        {
            [button setTitle:@" 作业" forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"icon_vacation"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"icon_vacation_focus"] forState:UIControlStateSelected];
        }
        if(i == 2)
        {
            [button setTitle:@" 通知" forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"icon_emergency"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"icon_emergency_focus"] forState:UIControlStateSelected];
        }
        if(i == 0)
        {
            [button setTitle:@" 动态" forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"icon_meet"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"icon_meet_focus"] forState:UIControlStateSelected];
        }
        [subview addSubview:button];
    }
    
    UIButton* button  = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = 2003;
    button.frame = CGRectMake(0, 98, SCREEN_WIDTH, 44);
    button.titleLabel.font = FONT_13;
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setTitleColor:BLACK forState:UIControlStateNormal];
    [button setBackgroundImage:[Utils imageFromColor:WHITE] forState:UIControlStateDisabled];
    [button setBackgroundImage:[Utils imageFromColor:WHITE] forState:UIControlStateNormal];
    [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(showClassChoose) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"发送班级: " forState:UIControlStateNormal];
    
    label = [[UILabel alloc] init];
    label.frame = CGRectMake(70, 0, SCREEN_WIDTH - 110, 44);
    label.textColor = BLUE;
    label.font = FONT_13;
    label.tag = 100;
    [button addSubview:label];
    
    UIButton* arrow = [UIButton buttonWithType:UIButtonTypeCustom];
    arrow.frame = CGRectMake(SCREEN_WIDTH - 48, 0, 48, 44);
    [arrow setImage:[UIImage imageNamed:@"ic_arrow"] forState:UIControlStateNormal];
    [button addSubview:arrow];
    
    CALayer* layer = [[CALayer alloc] init];
    layer.frame = CGRectMake(0, 44, SCREEN_WIDTH, 10);
    layer.backgroundColor = BG.CGColor;
    [button.layer addSublayer:layer];
    [subview addSubview:button];
    
    layer = [[CALayer alloc] init];
    layer.frame = CGRectMake(0, 0, SCREEN_WIDTH, .5);
    layer.backgroundColor = BG.CGColor;
    [button.layer addSublayer:layer];
    
    int yPos = 152;
    UITextView* textview = [[UITextView alloc] init];
    textview.frame = CGRectMake(0, yPos, SCREEN_WIDTH, 80);
    textview.tag = 2004;
    textview.font = FONT_13;
    textview.textColor = LINE;
    textview.text = @"请输入信息";
    textview.delegate = self;
    textview.contentInset = UIEdgeInsetsMake(5.0f, 0, 5.0f, 0);
    [scrollview addSubview:textview];
    
    label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, yPos + 80, SCREEN_WIDTH, 20);
    label.font = FONT_12;
    label.tag = 234;
    label.textColor = GRAY;
    label.textAlignment = NSTextAlignmentRight;
    label.text = @"0/300      ";
    label.backgroundColor = WHITE;
    [scrollview addSubview:label];
    
    layer = [[CALayer alloc] init];
    layer.frame = CGRectMake(0, 20, SCREEN_WIDTH, 10);
    layer.backgroundColor = BG.CGColor;
    [label.layer addSublayer:layer];
    
    
    yPos += 120;
    width = (SCREEN_WIDTH - 35)/4;
    for(int i = 0; i < 9; i++)
    {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 3000 + i;
        button.frame = CGRectMake(10 + (5 + width)*(i%4), yPos + (width + 5)*(i/4), width, width);
        button.layer.masksToBounds = YES;
        button.imageView.contentMode = UIViewContentModeScaleAspectFill;
        if(i > 0) button.hidden = YES;
        [button addTarget:self action:@selector(chooseImage:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundImage:[UIImage imageNamed:@"ic_addphoto"] forState:UIControlStateNormal];
        [scrollview addSubview:button];
    }
    
    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    [singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [self.view addGestureRecognizer:singleTapGestureRecognizer];
}

- (void)textViewDidChange:(UITextView *)textView
{
    UILabel* label = (UILabel*)[self.view viewWithTag:234];
    label.text = [NSString stringWithFormat:@"%d/300        ",textView.text.length];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        return NO;
    }
    
    if (textView.text.length - range.length + text.length > 300) {
        return NO;
    }
    
    return YES;
}
- (void)singleTap:(UIGestureRecognizer*)gestureRecognizer
{
    UITextView* textfield = (UITextView*)[self.view viewWithTag:2004];
    [textfield resignFirstResponder];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UITextView* textfield = (UITextView*)[self.view viewWithTag:2004];
    [textfield resignFirstResponder];
}

- (void) modifyName
{
    UITextView* textfield = (UITextView*)[self.view viewWithTag:2004];
    if(textfield.text.length == 0 || [textfield.text isEqualToString:@"请输入信息"])
    {
        [self showAllTextDialog:@"请输入内容"];
        return;
    }
    [textfield resignFirstResponder];
    
    if(_mSelectClass.count == 0)
    {
        [self showAllTextDialog:@"请选择班级"];
        return;
    }
    
    NSString* type = @"";
    if(((UIButton*)[self.view viewWithTag:2000]).isSelected) type = @"4";
    if(((UIButton*)[self.view viewWithTag:2001]).isSelected) type = @"1";
    if(((UIButton*)[self.view viewWithTag:2002]).isSelected) type = @"2";
    if(type.length == 0)
    {
        [self showAllTextDialog:@"请选择类型"];
        return;
    }
    
    NSMutableDictionary* param = [[NSMutableDictionary alloc] init];
    [param setValue:APPDELEGATE.mMemberInfo[@"id"] forKey:@"id"];
    [param setValue:[_mSelectClass componentsJoinedByString:@","] forKey:@"class_id"];
    [param setValue:@"" forKey:@"title"];
    [param setValue:textfield.text forKey:@"content"];
    [param setValue:type forKey:@"type"];
    [param setValue:@"0" forKey:@"receipt"];
    if(![type isEqualToString:@"4"]) [param setValue:@"1" forKey:@"receipt"];
    [self sendHttpImage:param];
}

- (void) sendHttpImage:(NSMutableDictionary*)param
{
    [self showWaitDialog];
    [param setValue:[Utils md5:[NSString stringWithFormat:@"%@ylbb",@"send_post"]] forKey:@"sec"];
    // 初始化Manager
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 不加上这句话，会报“Request failed: unacceptable content-type: text/plain”错误，因为我们要获取text/plain类型数据
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    // post请求
    [manager POST:[NSString stringWithFormat:@"http://120.27.129.233/ylbb/api/%@",@"send_post"]
       parameters:param constructingBodyWithBlock:^(id  _Nonnull formData) {
           for(int i = 0; i < _mImgData.count; i++)
           {
               NSString* name = [NSString stringWithFormat:@"attach%d",i + 1];
               [formData appendPartWithFileData:_mImgData[i] name:name fileName:name mimeType:@"image/jpeg"];
           }
       } progress:^(NSProgress * _Nonnull uploadProgress) {
           // 这里可以获取到目前的数据请求的进度
           
       } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
           NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
           if([dic[@"statusCode"] intValue] == 0)
           {
               MainViewController* subview = APPDELEGATE.mMainViewController;
               if(((UIButton*)[self.view viewWithTag:2000]).isSelected)
               {
                   [APPDELEGATE setTabInit:YES tab:0];
                   [subview buttonClick:(UIButton*)[subview.view viewWithTag:2000]];
               }
               if(((UIButton*)[self.view viewWithTag:2001]).isSelected)
               {
                   [APPDELEGATE setTabInit:YES tab:1];
                   [subview buttonClick:(UIButton*)[subview.view viewWithTag:2001]];
               }
               if(((UIButton*)[self.view viewWithTag:2002]).isSelected)
               {
                   [APPDELEGATE setTabInit:YES tab:2];
                   [subview buttonClick:(UIButton*)[subview.view viewWithTag:2002]];
               }
               
               [self.navigationController popViewControllerAnimated:YES];
           }
           else
           {
  //             NSString* str = [dic[@"message"] stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
               [APPDELEGATE showDialog:dic[@"message"]];
           }
           [self dissmissDialog];
       } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
           [self showAllTextDialog:@"网络状况不佳，请稍后重试..."];
           [self dissmissDialog];
       }];
}


- (void) httpCallback:(NSDictionary*)data flag:(int)flag
{
    
}

- (void) setImg
{
    int count = _mImg.count;
    for(int i = 0; i < 9; i++)
    {
        UIButton* button = (UIButton*)[self.view viewWithTag:3000 + i];
        if(i < count)
        {
            button.hidden = NO;
            [button setBackgroundImage:_mImg[i] forState:UIControlStateNormal];
        }
        else if(i == count)
        {
            button.hidden = NO;
            [button setBackgroundImage:[UIImage imageNamed:@"ic_addphoto"] forState:UIControlStateNormal];
        }
        else
        {
            button.hidden = YES;
            [button setBackgroundImage:[UIImage imageNamed:@"ic_addphoto"] forState:UIControlStateNormal];
        }
    }
}

- (void) chooseImage:(UIButton*)btn
{
    [(UITextView*)[self.view viewWithTag:2004] resignFirstResponder];
    
    _mSelected = btn.tag - 3000;
    UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:@"选择图片"
                                                       delegate:self
                                              cancelButtonTitle:@"返回"
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:@"从相册获取",@"拍照", nil];
    if(_mImgData.count > _mSelected) [sheet addButtonWithTitle:@"删除"];
    [sheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0)
    {
        if(_mImgData.count == 9)
        {
            [self showAllTextDialog:@"已达最大，请先删除"];
            return;
        }
        
        QBImagePickerController *imagePickerController = [QBImagePickerController new];
        imagePickerController.delegate = self;
        imagePickerController.mediaType = QBImagePickerMediaTypeImage;
        imagePickerController.allowsMultipleSelection = YES;
        imagePickerController.showsNumberOfSelectedAssets = YES;
        imagePickerController.minimumNumberOfSelection = 0;
        imagePickerController.maximumNumberOfSelection = 9 - _mImgData.count;
        
        [self presentViewController:imagePickerController animated:YES completion:NULL];
    }
    else if(buttonIndex == 1)
    {
        if(_mImgData.count == 9)
        {
            [self showAllTextDialog:@"已达最大，请先删除"];
            return;
        }
        
        UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        imagePicker.allowsEditing = NO;
        
        [self presentModalViewController:imagePicker animated:YES];
    }
    else if(buttonIndex == 3)
    {
        [_mImgData removeObjectAtIndex:_mSelected];
        [_mImg removeObjectAtIndex:_mSelected];
        [self setImg];
    }
}

- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets
{
    NSLog(@"Selected assets:");
    NSLog(@"%@", assets);
    
    if(_mImgData.count == 9)
    {
        return;
    }
    
    for(int i = 0; i < assets.count; i++)
    {
        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
        options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
        
        [[PHImageManager defaultManager] requestImageForAsset:(PHAsset*)assets[i] targetSize:[UIScreen mainScreen].bounds.size contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {
            result = [Utils imageCompressForWidth:result targetWidth:1080];
            [_mImgData addObject:UIImageJPEGRepresentation(result, 0.3)];
            [_mImg addObject:[Utils imageCompressForWidth:result targetWidth:240]];
            [self setImg];
        }];
    }
    
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController
{
    NSLog(@"Canceled.");
    
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        //        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
    
    image = [Utils imageCompressForWidth:image targetWidth:1080];
    NSData* data = UIImageJPEGRepresentation(image, 0.3);
    
    [_mImgData addObject:data];
    [_mImg addObject:[Utils imageCompressForWidth:image targetWidth:240]];
    [self setImg];
    [self dismissModalViewControllerAnimated:YES];
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
   
    if([textView.text isEqualToString:@"请输入信息"])
    {
        textView.text= @"";
        textView.textColor = BLACK;
    }
}

- (void) buttonClick:(UIButton*)button
{
    [(UITextView*)[self.view viewWithTag:2004] resignFirstResponder];
    ((UIButton*)[self.view viewWithTag:2000]).selected = NO;
    ((UIButton*)[self.view viewWithTag:2001]).selected = NO;
    ((UIButton*)[self.view viewWithTag:2002]).selected = NO;
    
    button.selected = YES;
}

- (void) showClassChoose
{
    [(UITextView*)[self.view viewWithTag:2004] resignFirstResponder];
    
    _inputView = [[UIControl alloc] init];
    _inputView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT + 64);
    _inputView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3];
    [_inputView addTarget:self action:@selector(removeSelf) forControlEvents:UIControlEventTouchUpInside];
    [APPDELEGATE.window addSubview:_inputView];
    
    NSArray* array = APPDELEGATE.mMemberInfo[@"classes"];
    UIView* subview = [[UIView alloc] init];
    subview.frame = CGRectMake(0, 0, SCREEN_WIDTH*2/3, (array.count + 1)*44);
    subview.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
    [_inputView addSubview:subview];
    
    int count = array.count;
    for(int i = 0; i < count; i++)
    {
        NSDictionary* item = array[i];
        
        NSString* className = item[@"class_alias"];
        if([Utils isStringNullOrEmpty:className]) className = item[@"class_name"];
        
        UIButton* button  = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 4000 + i;
        button.frame = CGRectMake(0, 44*i, SCREEN_WIDTH*2/3, 44);
        button.titleLabel.font = FONT_15;
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [button setTitleColor:BLACK forState:UIControlStateNormal];
        [button setBackgroundImage:[Utils imageFromColor:WHITE] forState:UIControlStateNormal];
        [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(classClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:className forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"check_win"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"check_win_focus"] forState:UIControlStateSelected];
        [subview addSubview:button];
        
        CALayer* layer = [[CALayer alloc] init];
        layer.frame = CGRectMake(0, 43.5, SCREEN_WIDTH*2/3, .5);
        layer.backgroundColor = LINE.CGColor;
        [button.layer addSublayer:layer];
        
        if([self isClassChoose:[item[@"id"] description] type:0])
        {
            button.selected = YES;
        }
    }
    
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 44*count, SCREEN_WIDTH*2/3, 44);
    button.titleLabel.font = FONT_15;
    [button setTitle:@"确定" forState:UIControlStateNormal];
    [button setTitleColor:WHITE forState:UIControlStateNormal];
    [button addTarget:self action:@selector(confirmClass) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[Utils imageFromColor:BLUE] forState:UIControlStateNormal];
    [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
    [subview addSubview:button];
}

- (void) confirmClass
{
    if(_mSelectClass.count == 0)
    {
        [self showAllTextDialog:@"请选择至少一个班级"];
    }
    else
    {
        [self removeSelf];
        
        NSMutableString* str = [NSMutableString stringWithCapacity:256];
        
        NSArray* array = APPDELEGATE.mMemberInfo[@"classes"];
        for(NSDictionary* item in array)
        {
            if([self isClassChoose:[item[@"id"] description] type:0])
            {
                if(str.length) [str appendString:@","];
                NSString* className = item[@"class_alias"];
                if([Utils isStringNullOrEmpty:className]) className = item[@"class_name"];
                [str appendString:className];
            }
        }
        
        ((UILabel*)[[self.view viewWithTag:2003] viewWithTag:100]).text = str;
    }
}

- (void) classClick:(UIButton*)btn
{
    int index = (int)(btn.tag - 4000);
    NSDictionary* item = APPDELEGATE.mMemberInfo[@"classes"][index];
    
    if([self isClassChoose:[item[@"id"] description] type:1]) btn.selected = NO;
    else
    {
        [_mSelectClass addObject:[item[@"id"] description]];
        btn.selected = YES;
    }
}

- (BOOL) isClassChoose:(NSString*)classId type:(int)type
{
    for(int i = 0; i < _mSelectClass.count; i++)
    {
        if([_mSelectClass[i] isEqualToString:classId])
        {
            if(type) [_mSelectClass removeObjectAtIndex:i];
            return YES;
        }
    }
    
    return NO;
}

- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    int keyboardHeight = keyboardF.size.height;
    
    UIScrollView* scrollview = (UIScrollView*)[self.view viewWithTag:10000];
    scrollview.frame = CGRectMake(0, -60, SCREEN_WIDTH, SCREEN_HEIGHT - keyboardHeight + 60);
}

- (void)keyboardWillChangeFrameHide:(NSNotification *)notification
{
    UIScrollView* scrollview = (UIScrollView*)[self.view viewWithTag:10000];
    scrollview.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}

- (void) viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void) viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void) removeSelf
{
    [_inputView removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
